package Servlet;
import java.io.IOException;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.catalina.core.ApplicationContext;

/**
 * Servlet implementation class addServlet
 */
@WebServlet("/addServlet")
public class addServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public addServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String name=request.getParameter("name");
		int age=Integer.parseInt(request.getParameter("age"));
		String sex=request.getParameter("sex");
		String birthday=request.getParameter("birthday");
		String pwd=request.getParameter("pwd");
    	String sql="insert into test(name,age,sex,birthday,pwd)"+
		           "values('"+name+"','"+age+"','"+sex+"','"+birthday+"','"+pwd+"')";
    	PrintWriter out=response.getWriter();
    	int number =0;
	    Connection con=null;
	    Statement st=null;
	    int result=0;
	    if(request.getSession().getAttribute("number") == null)
		{
			number=1;
		}
		else
		{
			number=Integer.parseInt((String)request.getSession().getAttribute("number"));
			if(request.getSession().isNew())
			{
				number=number+1;
			}
		}
		this.getServletContext().setAttribute("number",String.valueOf(number));
        
	    try {
	    	String driverClass="com.mysql.jdbc.Driver";
	    	String url="jdbc:mysql://localhost:3306/rrr";
	    	String username="root";
	    	String password="lishanshan";
	    	Class.forName(driverClass); 
       	    con= DriverManager.getConnection(
       			 url,username,password);
       	    st= con.createStatement();
       	    result =st.executeUpdate(sql);
       	    response.setContentType("text/html;charset=UTF-8");
       	    out.println("<a href=\""+request.getContextPath()+"/register.jsp\">返回人员列表</a>");
       	    out.flush();
       	    out.close();
	    }catch(SQLException e) {
	    	e.printStackTrace();
	    }catch(ClassNotFoundException e) {
	    	e.printStackTrace();
	    }finally {
	    	try {
	    		st.close();
	    		con.close();
	    	}catch(SQLException E) {
	    		E.printStackTrace();
	    	}
	      }
	  }
}
